import pika
import time

# 连接参数
credentials = pika.PlainCredentials('XuYixuan', 'Xyx20040422')
connection = pika.BlockingConnection(pika.ConnectionParameters('82.156.163.134', 5672, '/', credentials))
channel = connection.channel()

# 声明一个持久化队列
channel.queue_declare(queue='sensor_data', durable=True)

# 发送消息
for sensor_id in range(3):
    data = f"传感器{sensor_id}, 温度{20+sensor_id}, 时间{time.time()}"
    channel.basic_publish(
        exchange='',
        routing_key='sensor_data',
        body=data.encode('utf-8'),
        properties=pika.BasicProperties(
            delivery_mode=2,  # 使消息持久化
        )
    )
    print(f"发送消息: {data}")

# 关闭连接
connection.close()